package com.wutuo.aggragation.roster.controller;

import cn.apestar.agg.common.security.BaseAuthController;
import cn.apestar.core.base.BaseMessage;
import com.wutuo.child.api.response.ChildResponse;
import com.wutuo.roster.api.RosterAPIService;
import com.wutuo.roster.api.request.RosterRequest;
import com.wutuo.roster.api.request.SearchRosterRequest;
import com.wutuo.roster.api.response.RosterResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 花名册 <br>
 * 详细说明.
 * <p>
 * Copyright: Copyright (c) 2016年1月26日 下午10:21:39
 * <p>
 *
 * @author lvh
 * @version 1.0.0
 */
@RestController @RequestMapping(value = "/roster", produces = "application/json")
public class RosterController extends BaseAuthController {

    private Logger logger = LoggerFactory.getLogger(RosterController.class);

    @Autowired private RosterAPIService rosterService;

    /**
     * 新增花名册
     *
     * @param request
     */
    @RequestMapping(value = "/addorUpdate", method = RequestMethod.POST) public Object create(
        @RequestBody @Valid RosterRequest request) {

        if (request.getCmInstitutionId() == null) {
            request.setCmInstitutionId(this.getInstitutionId());
        }
        rosterService.addorUpdate(request);
        return new BaseMessage();
    }

    /**
     * 删除花名册
     *
     * @param id
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.GET) public Object del(
        @PathVariable("id") Long id) {

        rosterService.del(id);
        return new BaseMessage();
    }

    /**
     * 查询花名册根据孩子的ID
     *
     * @param id
     */
    @RequestMapping(value = "/child/{id}", method = RequestMethod.GET) public Object get(
        @PathVariable("id") Long id) {

        return rosterService.getRosterById(id);
    }

    /**
     * 查询花名册
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/lisForPage", method = RequestMethod.POST)
    public List<RosterResponse> lisForPage(@RequestBody SearchRosterRequest request) {

        List list = rosterService.lisForPage(request);
        return list;
    }

    /**
     * 查询花名册
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/listChildByState", method = RequestMethod.POST)
    public List<RosterResponse> listChildByState(@RequestBody SearchRosterRequest request) {

        List list = rosterService.listChildByState(request);
        return list;
    }

    /**
     * 根据父母电话查询孩子的花名册信息
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/child/list/{phone}", method = RequestMethod.GET)
    public List<RosterResponse> getRoasterByPphone(@PathVariable("phone") String phone) {

        SearchRosterRequest request = new SearchRosterRequest();
        request.setpPhone(phone);
        request.setIsOut(true);
        List list = rosterService.getChildListByPidNoTask(request);
        return list;
    }

    /**
     * 根据父母电话查询孩子的花名册信息
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/child/all/{phone}", method = RequestMethod.GET)
    public List<RosterResponse> getRoasterByAllPphone(@PathVariable("phone") String phone) {

        SearchRosterRequest request = new SearchRosterRequest();
        request.setpPhone(phone);
        List list = rosterService.getChildListByPidNoTask(request);
        return list;
    }

    /**
     * 根据当前登录父母电话查询孩子的花名册信息
     *
     * @return
     */
    @RequestMapping(value = "/childByPhone", method = RequestMethod.GET)
    public List<RosterResponse> childByPhone() {

        SearchRosterRequest request = new SearchRosterRequest();
        request.setpPhone(getCurrentUser().getPhone());
        request.setIsOut(true);
        List list = rosterService.getChildListByPid(request);
        return list;
    }

    /**
     * 查询家长屁孩列表
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/parent/list/{id}", method = RequestMethod.GET)
    public List<ChildResponse> getChildListByPid(@PathVariable("id") Long id) {

        SearchRosterRequest request = new SearchRosterRequest();
        request.setParentId(id);
        List list = rosterService.getChildListByPid(request);
        return list;
    }

    /**
     * 查询家长屁孩列表
     *
     * @return
     */
    @RequestMapping(value = "/parent/list", method = RequestMethod.GET)
    public List<ChildResponse> getChildList() {

        SearchRosterRequest request = new SearchRosterRequest();
        request.setParentId(getCurrentUserId());
        List list = rosterService.getChildListByPid(request);
        return list;
    }

    /**
     * 增加花名册标签
     *
     * @return
     */
    @RequestMapping(value = "/add/tag/{tagId}/user/{userId}", method = RequestMethod.GET)
    public Object addRoasterAndTagRelations(@PathVariable("tagId") Long tagId,
        @PathVariable("userId") String userId) {

        rosterService.addRoasterAndTagRelations(tagId, userId);
        return new BaseMessage();
    }

    /**
     * 更新花名册状态
     *
     * @return
     */
    @RequestMapping(value = "/updateRosterState/id/{userId}/state/{type}", method = RequestMethod.GET)
    public Object updateRosterState(@PathVariable("userId") String userId,
        @PathVariable("type") String type) {

        if (!StringUtils.isEmpty(userId)) {
            rosterService.updateRosterState(userId, type);
        }
        return new BaseMessage();
    }

    /**
     * 更新花名册状态
     *
     * @return
     */
    @RequestMapping(value = "/updateRosterState", method = RequestMethod.POST)
    public Object updateRosterManageState(String userId, String type) {

        if (!StringUtils.isEmpty(userId)) {
            rosterService.updateRosterState(userId, type);
        }
        return new BaseMessage();
    }
}
